import os
import requests

# 假设这是Ragflow知识库文件上传接口的URL
upload_url = "http://18.0.44.51:8900/api/v1/datasets/e8c13a4e2a5b11f086640242ac1f0006/documents"
# 如果有认证token，请替换下面的字符串
headers = {"Authorization": "Bearer ragflow-dmMjFiMjQwMmIxMzExZjBhNmNiMDI0Mm"}

def upload_file(file_path):
    """上传单个文件到指定的知识库"""
    with open(file_path, 'rb') as file:
        files = {'file': (os.path.basename(file_path), file)}
        response = requests.post(upload_url, headers=headers, files=files)
        if response.status_code == 200:
            print(f"成功上传: {file_path}")
        else:
            print(f"上传失败: {file_path}, 状态码: {response.status_code}")

def traverse_and_upload(directory):
    """递归遍历目录及其子目录，并上传所有文件"""
    for root, dirs, files in os.walk(directory):
        for name in files:
            file_path = os.path.join(root, name)
            upload_file(file_path)

if __name__ == "__main__":
    # 替换为你的本地目录路径
    local_directory = "/Users/aresen/Downloads/工信部/文件发布_副本"
    traverse_and_upload(local_directory)
